local mingju= fk.CreateSkill {
    name = "steam__mingju",
    tags = {Skill.AttachedKingdom},
    attached_kingdom = {"shu"},
  }

Fk:loadTranslationTable{
    ["steam__mingju"] = "明举",
    [":steam__mingju"] = "蜀势力技，你可卜算5并按<a href='mingju_longhun'>龙魂规则</a>使用其中一张，然后本轮卜算数-1。",

    ["#steam__mingju-prompt"] = "明举：你可以按红桃当【桃】；方块当火【杀】；梅花当【闪】；黑桃当【无懈可击】的规则使用其中一张",
    ["mingju_longhun"] = "红桃当【桃】；方块当火【杀】；梅花当【闪】；黑桃当【无懈可击】。",
}

--- 询问player是否要发动一个视为技。（额还不太行）
---
--- 如果发动的话，那么会执行一下技能的onUse函数，然后返回选择的牌和目标等。
---@param player ServerPlayer @ 询问目标
---@param params AskToUseActiveSkillParams @ 各种变量
---@return boolean, { cards: integer[], targets: ServerPlayer[], interaction: any }? @ 返回第一个值为是否成功发动，第二值为技能选牌、目标等数据
local function askToUseViewasSkill(room,player, params)
    params.prompt = params.prompt or ""
    params.cancelable = (params.cancelable == nil) and true or params.cancelable
    params.no_indicate = (params.no_indicate == nil) and true or params.no_indicate
    params.extra_data = params.extra_data or Util.DummyTable
    params.skip = params.skip or params.extra_data.skipUse
    ---@diagnostic disable-next-line assign-type-mismatch
    local skill = Fk.skills[params.skill_name] ---@type ActiveSkill | ViewAsSkill

  
    local command = "AskForUseActiveSkill"
    local data = {params.skill_name, params.prompt, params.cancelable, params.extra_data}
  
    Fk.currentResponseReason = params.extra_data.skillName
    local req = Request:new(player, command)
    req:setData(player, data)
    req.focus_text = params.extra_data.skillName or params.skill_name
    local result = req:getResult(player)
    Fk.currentResponseReason = nil
    Fk.currentResponsePattern = nil
    if result == "" then
      return false
    end
  
    data = result
    local card = data.card
    local targets = data.targets
    local card_data = card
    local selected_cards = card_data.subcards
    local interaction
    if not params.no_indicate then
      room:doIndicate(player.id, targets)
    end
  
    if skill.interaction then
      interaction = data.interaction_data
      skill.interaction.data = interaction
    end
  
    if skill:isInstanceOf(ActiveSkill) and not params.skip then
      ---@cast skill ActiveSkill
      skill:onUse(room, SkillUseData:new {
        from = player,
        cards = selected_cards,
        tos = table.map(targets, Util.Id2PlayerMapper),
      })
    end
    if skill:isInstanceOf(ViewAsSkill) then
        local usecard=skill:viewAs(player, selected_cards)
        return {
          cards = usecard,
          targets = table.map(targets, Util.Id2PlayerMapper),
          interaction = interaction
        }
    end
  
    return true, {
      cards = selected_cards,
      targets = table.map(targets, Util.Id2PlayerMapper),
      interaction = interaction
    }
  end


mingju:addEffect("active", {
    prompt = "#steam__mingju-prompt",
    can_use = function (self, player)
      return player:usedSkillTimes(mingju.name, Player.HistoryTurn) == 0
    end,
    card_num = 0,
    target_num = 0,
    card_filter = Util.FalseFunc,
    target_filter = Util.FalseFunc,
    on_use = function (self, room, effect)
      local player = effect.from
      --卜算
      local n=5-player:getMark("mingjutimes-phase")
      local cards=room:getNCards(n)
      room:askToGuanxing(player, {
        cards = cards, skill_name = mingju.name, skip = false,
      })
      room:addPlayerMark(player,"mingjutimes-phase")
      local touse={}
      for _,card in ipairs(cards) do
        table.insert(touse,card)
      end
      room:setPlayerMark(player,"mingju-phase",touse)
      --按龙魂规则使用
      local dat=askToUseViewasSkill(room,player, 
      {skill_name = "#steam__mingju_active", 
      prompt = "按“龙魂”规则使用其中一张。"})
      room:setPlayerMark(player,"mingju-phase",0)

      if dat and dat.cards then
      local use = {
        from = player,
        tos = dat.targets,
        card = dat.cards,
      }
        room:useCard(use)
      end
  
    end
})



mingju:addEffect(fk.AskForCardUse, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return
      target == player and
      player:hasSkill(mingju.name) and
      data.pattern and
      Exppattern:Parse(data.pattern):matchExp( "peach,slash,jink,nullification")
  end,
  on_use = function(self, event, target, player, data)
    ---@type string
    local skillName = mingju.name
    local room = player.room
          --卜算
    local n=5-player:getMark("mingjutimes-phase")
    local cards=room:getNCards(n)
    room:askToGuanxing(player, {
      cards = cards, skill_name = mingju.name, skip = false,
          })
    room:addPlayerMark(player,"mingjutimes-phase")
    local touse={}
    for _,card in ipairs(cards) do
      table.insert(touse,card)
    end
    room:setPlayerMark(player,"mingju-phase",touse)
    room:setPlayerMark(player,"mingju-pattern",data.pattern)
    local dat=askToUseViewasSkill(room,player, 
    {skill_name = "#steam__mingju_active", 
    prompt = "按“龙魂”规则使用其中一张。"})
    room:setPlayerMark(player,"mingju-phase",0)
    room:setPlayerMark(player,"mingju-pattern",nil)
    if dat and dat.cards then
      local card = dat.cards
      card.skillName = skillName
      data.result = {
        from = player,
        card = card,
      }
      if data.eventData then
        data.result.toCard = data.eventData.toCard
        data.result.responseToEvent = data.eventData.responseToEvent
      end
      return true
    end
  end,
})
--








return mingju